<?php

class categoryControl extends commonControl {

    function index() {
        $db = M("category");
        $cat = $db->all(); //查找所有栏目
        header("Content-type:text/html;charset=utf-8");
        $category = arr::channel($cat, "cat_id", "cat_pid");
        $this->assign("category", $category);
        $this->display();
    }

    //添加栏目
    function add_show() {
        $list = dir::tree(C("index_tpl") . '/' . C("index_tpl_style"));
        
        $this->assign("filename", $list);
        $this->display();
    }

    //将新增加栏目数据插入表中
    function add() {
        $db = M("category");
        if ($db->add()) {
            $this->success("栏目添加成功", "index");
        } else {
            $this->error("添加失败");
        }
    }

    //编辑视图
    function edit_show() {
        $db = M("category");
	$this->assign("cat_all",arr::channel($db->all(),"cat_id","cat_pid"));
	$category = $db->find("cat_id='{$_GET['cat_id']}'");
	$this->assign("category",$category);
        
        $this->assign("list", $category['list_tpl']);
        $this->assign("arc", $category['arc_tpl']);
        
        $list = dir::tree(C("index_tpl") . '/' . C("index_tpl_style"));        
        $this->assign("filename", $list);
        $this->display();
    }

    //编辑栏目数据
    function edit() {
        $db = M("category");
        p($_POST);
        if ($db->save() >= 0) {
            //$this->success("栏目编辑成功", "index");
        } else {
            //$this->error("栏目编辑失败");
        }
    }

    //删除栏目
    function del() {
        $cat_id = $_GET['cat_id'];
        $db = M("category");
        if ($db->find("cat_pid='$cat_id'")) {
            $this->error("请先删除子栏目");
        }
        $db->table("arc")->del("cat_id=$cat_id");
        if ($db->del($cat_id)) {
            $this->success("删除成功", "index");
        }
        $this->error("删除失败");
    }
}

?>